package utils

import (
	"encoding/binary"
	"io"
	"os"
	"strconv"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func Str2U32(data string) (result uint32) {
	data1, err := strconv.Atoi(data)
	if err != nil || data1 < 0 {
		result = 0
		return
	}
	result = uint32(data1)
	return
}

func U32toStr(data uint32) string {
	v := uint64(data)
	return strconv.FormatUint(v, 10)
}

func ReadString(r io.Reader) string {
	var buf []byte
	var b byte = 0xff
	for b != 0 {
		binary.Read(r, binary.LittleEndian, &b)
		buf = append(buf, b)
	}
	return string(buf)
}
